package com.ng.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jfinal.kit.StrKit;
import com.ng.biz.model.domain.DataLogInput;
import com.ng.biz.model.domain.HttpApi;
import com.ng.biz.service.IDataErrorLog;
import com.ng.biz.service.IDataFlow;
import com.ng.biz.service.IDataLog;
import com.ng.biz.service.IHttpApi;
import com.ng.common.enums.DataLogStatusEnum;
import com.ng.common.model.response.R;
import com.ng.common.util.IscUtil;
import com.ng.common.util.ParamFieldParseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.Executors;

@Service
@Slf4j
public class HttpApiImpl implements IHttpApi {
	
	@Autowired
	private IDataFlow dataFlowService;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
    @Autowired
    private IDataLog dataLogService;
    
    @Autowired
    private IDataErrorLog dataErrorLogService;
	
	// 添加常量定义
	private static final String AUTH_TYPE_BEARER = "bearer_token";
	private static final String AUTH_TYPE_BASIC = "basic_auth";
	private static final String AUTH_TYPE_NONE = "no_auth";
	
	private static final String BODY_TYPE_FORM = "form-data";
	private static final String BODY_TYPE_URLENCODED = "x-www-form-urlencoded";
	private static final String BODY_TYPE_JSON = "raw-json";
	private static final String BODY_TYPE_XML = "raw-xml";
	private static final String UTF_8 = "UTF-8";

	// 添加默认超时时间
	private static final int DEFAULT_TIMEOUT = 5000;
	
	@Override
	public R<JSONObject> execute(HttpApi httpApi,JSONObject params) {
		if (httpApi == null) {
			return R.failed("HTTP接口配置不能为空");
		}
		
		if(StrKit.equals("1", httpApi.getClientType())) {
			List<JSONObject> bodyParams = JSON.parseObject(httpApi.getRawContent(), new TypeReference<List<JSONObject>>() {});
			if(bodyParams!=null && bodyParams.size()>0) {
				JSONObject jsonObject=ParamFieldParseUtil.parseValuesByFields(bodyParams, JSONObject.toJSONString(params));
				if(jsonObject!=null && jsonObject.size()>0) {
					try {
						String result_str=IscUtil.getData(httpApi.getHost(),httpApi.getAppKey(),httpApi.getAppSecret(),httpApi.getCamsApi(),httpApi.getUrl(),JSONObject.toJSONString(params));
						if(!StrKit.isBlank(result_str)) {
							
							JSONObject iscJSONObject=JSONObject.parseObject(result_str);
							if(iscJSONObject==null || iscJSONObject.isEmpty()) {
								return R.failed("编辑综合安防管理平台HTTP数据源无响应.");
							}
							
							JSONObject result = new JSONObject();
							
							// 设置状态码
							result.put("statusCode", iscJSONObject.getInteger("code")==0?200:iscJSONObject.getInteger("code"));
							
							// 设置响应体
							result.put("body", result_str);
							
							// 设置响应头
							result.put("headers", new ArrayList<>());
							
							return R.ok(result);
						}else {
							return R.failed("编辑综合安防管理平台HTTP数据源无响应.");
						}
					} catch (Exception e) {
						log.error("PostStringArtemis 异常:{}",e.getMessage());
						return R.failed(e.getMessage());
					}
				}
			}
			return R.failed();
		}

		try {
			HttpRequestBase request = null;
			CloseableHttpClient client = null;
			
			try {
				// 构建请求客户端
				HttpClientBuilder builder = createHttpClientBuilder(httpApi);
				client = builder.build();
				
				// 创建并配置请求
				request = createAndConfigureRequest(httpApi,params);
				
				// 执行请求并处理响应
				return executeRequestAndHandleResponse(client, request);
				
			} finally {
				// 确保资源释放
				if (request != null) {
					request.releaseConnection();
				}
				if (client != null) {
					client.close();
				}
			}
		} catch (IllegalArgumentException e) {
			log.warn("HTTP请求参数错误", e);
			return R.failed("请求参数错误: " + e.getMessage());
		} catch (Exception e) {
			log.error("HTTP请求执行失败", e);
			return R.failed("请求失败: " + e.getMessage());
		}
	}
	
	private HttpClientBuilder createHttpClientBuilder(HttpApi httpApi) {
		HttpClientBuilder builder = HttpClientBuilder.create();
		
		// 配置超时
		int timeout = httpApi.getTimeout() > 0 ? httpApi.getTimeout() : DEFAULT_TIMEOUT;
		RequestConfig config = RequestConfig.custom()
			.setConnectTimeout(timeout)
			.setSocketTimeout(timeout)
			.build();
		builder.setDefaultRequestConfig(config);
		
		if(!StrKit.equals(AUTH_TYPE_NONE, httpApi.getAuthType())) {
		// 配置认证
		configureAuthentication(builder, httpApi);
		}
		return builder;
	}
	
	private HttpRequestBase createAndConfigureRequest(HttpApi httpApi,JSONObject params) {
		// 创建请求
		HttpRequestBase request = createRequest(httpApi);
		
		// 设置请求头
		setHeaders(request, httpApi);
		
		// 设置请求体
		if (request instanceof HttpEntityEnclosingRequestBase) {
			setRequestBody((HttpEntityEnclosingRequestBase) request, httpApi,params);
		}
		return request;
	}
	
	private R<JSONObject> executeRequestAndHandleResponse(CloseableHttpClient client, HttpRequestBase request) 
			throws Exception {
		try (CloseableHttpResponse response = client.execute(request)) {
			JSONObject result = new JSONObject();
			
			// 设置状态码
			result.put("statusCode", response.getStatusLine().getStatusCode());
			
			// 设置响应体
			HttpEntity entity = response.getEntity();
			result.put("body", entity != null ? EntityUtils.toString(entity) : "");
			
			// 设置响应头
			result.put("headers", extractResponseHeaders(response));
			
			return R.ok(result);
		}
	}
	
	private List<JSONObject> extractResponseHeaders(CloseableHttpResponse response) {
		List<JSONObject> responseHeaders = new ArrayList<>();
		for (Header header : response.getAllHeaders()) {
			JSONObject headerObj = new JSONObject();
			headerObj.put("name", header.getName());
			headerObj.put("value", header.getValue());
			responseHeaders.add(headerObj);
		}
		return responseHeaders;
	}
	
	private void configureAuthentication(HttpClientBuilder builder, HttpApi httpApi) {
		String authType = httpApi.getAuthType();
		if (authType == null) {
			return;
		}
		
		switch (authType) {
			case AUTH_TYPE_BEARER:
				configureBearerAuth(builder,httpApi);
				break;
			case AUTH_TYPE_BASIC:
				configureBasicAuth(builder, httpApi.getBasicAuthUsername(), httpApi.getBasicAuthPassword());
				break;
		}
	}
	
	//BearerAuth
	private void configureBearerAuth(HttpClientBuilder builder, HttpApi api) {
		
		if(StrKit.equals("0", api.getAuthBearerFromRedisRead())) {
			String token=api.getAuthBearerToken();
			if (StrKit.notBlank(token)) {
				List<Header> headers = new ArrayList<>();
				// 检查 token 是否已包含 Bearer 前缀
				String bearerToken = token.trim();
				if (!bearerToken.toLowerCase().startsWith("bearer ")) {
					bearerToken = "Bearer " + bearerToken;
				}
				headers.add(new BasicHeader("Authorization", bearerToken));
				builder.setDefaultHeaders(headers);
			}
		}
		else if(StrKit.equals("1", api.getAuthBearerFromRedisRead())) {
			String token=redisTemplate.opsForValue().get(api.getAuthBearerRedisReadKey());
		if (StrKit.notBlank(token)) {
			List<Header> headers = new ArrayList<>();
			// 检查 token 是否已包含 Bearer 前缀
			String bearerToken = token.trim();
			if (!bearerToken.toLowerCase().startsWith("bearer ")) {
				bearerToken = "Bearer " + bearerToken;
			}
			headers.add(new BasicHeader("Authorization", bearerToken));
			builder.setDefaultHeaders(headers);
			}
		}
	}
	
	//BasicAuth
	private void configureBasicAuth(HttpClientBuilder builder, String username, String password) {
		if (StrKit.notBlank(username) && StrKit.notBlank(password)) {
			CredentialsProvider provider = new BasicCredentialsProvider();
			provider.setCredentials(
				AuthScope.ANY,
				new UsernamePasswordCredentials(username, password)
			);
			builder.setDefaultCredentialsProvider(provider);
		}
	}
	
	private HttpRequestBase createRequest(HttpApi httpApi) {
		String url = buildUrl(httpApi);
		switch (httpApi.getMethod().toUpperCase()) {
			case "GET":
				return new HttpGet(url);
			case "POST":
				return new HttpPost(url);
			case "PUT":
				return new HttpPut(url);
			case "DELETE":
				return new HttpDelete(url);
			case "PATCH":
				return new HttpPatch(url);
			default:
				throw new IllegalArgumentException("不支持的HTTP方法: " + httpApi.getMethod());
		}
	}
	
	private String buildUrl(HttpApi httpApi) {
		StringBuilder url = new StringBuilder(httpApi.getUrl());
		List<JSONObject> params = JSON.parseObject(httpApi.getUrlParams(), new TypeReference<List<JSONObject>>() {});
		if (params != null && !params.isEmpty()) {
			JSONObject jsonObject = ParamFieldParseUtil.parseValuesByFields(params, JSONObject.toJSONString(new JSONObject()));
			if(jsonObject!=null && jsonObject.size()>0) {
			url.append(url.toString().contains("?") ? "&" : "?");
				
				int i=0;
				for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
					String key = entry.getKey();
					String value = entry.getValue() != null ? entry.getValue().toString() : "";
				if (i > 0) {
					url.append("&");
				}
					url.append(key).append("=").append(value);
					i++;
				}
			}
		}
		return url.toString();
	}
	
	//设置请求头
	private void setHeaders(HttpRequestBase request, HttpApi httpApi) {
		List<JSONObject> headers = JSON.parseObject(httpApi.getHeaders(), new TypeReference<List<JSONObject>>() {});
		if (headers != null) {
			JSONObject jsonObject = ParamFieldParseUtil.parseValuesByFields(headers, JSONObject.toJSONString(new JSONObject()));
			for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
				String key = entry.getKey();
				String value = entry.getValue() != null ? entry.getValue().toString() : "";
				if(!StrKit.isBlank(value)) {
					request.addHeader(key, value);
				}
			}
		}
	}
	
	
	private void setRequestBody(HttpEntityEnclosingRequestBase request, HttpApi httpApi,JSONObject params) {
		String bodyType = httpApi.getBodyType();
		if (bodyType == null) {
			return;
		}
		
		try {
			StringEntity entity = null;
			
			switch (bodyType) {
				case BODY_TYPE_FORM://form-data
				case BODY_TYPE_URLENCODED://x-www-form-urlencoded
					entity = createFormEntity(httpApi,params);
					break;
				case BODY_TYPE_JSON://raw-json
					List<JSONObject> bodyParams = JSON.parseObject(httpApi.getRawContent(), new TypeReference<List<JSONObject>>() {});
					if(bodyParams!=null && bodyParams.size()>0) {
						JSONObject jsonObject=ParamFieldParseUtil.parseValuesByFields(bodyParams, JSONObject.toJSONString(params));
						if(jsonObject!=null && jsonObject.size()>0) {
							entity = new StringEntity(JSONObject.toJSONString(jsonObject), ContentType.APPLICATION_JSON);
						}
					}
					break;
				case BODY_TYPE_XML://raw-xml
					entity = new StringEntity(httpApi.getRawContent(), ContentType.APPLICATION_XML);
					break;
			}
			if (entity != null) {
				request.setEntity(entity);
			}
		} catch (Exception e) {
			log.error("设置请求体失败", e);
			throw new IllegalArgumentException("设置请求体失败: " + e.getMessage());
		}
	}
	
	private StringEntity createFormEntity(HttpApi httpApi,JSONObject valueJSONObject) {
		List<JSONObject> bodyParams = JSON.parseObject(httpApi.getUrlParams(), new TypeReference<List<JSONObject>>() {});
		if(bodyParams==null || bodyParams.isEmpty()) {
			return null;
		}
		List<JSONObject> fields=ParamFieldParseUtil.assignValuesToFields(bodyParams , JSONObject.toJSONString(valueJSONObject));
		
		if (BODY_TYPE_FORM.equals(httpApi.getBodyType())) {
			// 使用 multipart/form-data
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			String fieldType="";
			for(JSONObject jsonObject:fields) {
				fieldType=jsonObject.getString("type");
				String identifier = jsonObject.getString("identifier");
				Object value = jsonObject.get("value");
				
				if (value == null) {
					continue;
				}
				
				if(StrKit.equals("dateString", fieldType) || StrKit.equals("timestampString", fieldType) || StrKit.equals("date", fieldType)) {
					builder.addPart(identifier, new StringBody(value.toString(), ContentType.TEXT_PLAIN));
				}
				else if(StrKit.equals("int", fieldType)) {
					builder.addPart(identifier, new StringBody(value.toString(), ContentType.TEXT_PLAIN));
				}
				else if(StrKit.equals("long", fieldType)) {
					builder.addPart(identifier, new StringBody(value.toString(), ContentType.TEXT_PLAIN));
				}
				else if(StrKit.equals("float", fieldType)) {
					builder.addPart(identifier, new StringBody(value.toString(), ContentType.TEXT_PLAIN));
				}
				else if(StrKit.equals("double", fieldType)) {
					builder.addPart(identifier, new StringBody(value.toString(), ContentType.TEXT_PLAIN));
				}
				else if(StrKit.equals("boolean", fieldType)) {
					builder.addPart(identifier, new StringBody(value.toString(), ContentType.TEXT_PLAIN));
				}
				else if(StrKit.equals("array", fieldType)) {
					builder.addPart(identifier, new StringBody(JSON.toJSONString(value), ContentType.APPLICATION_JSON));
				}
				else if(StrKit.equals("object", fieldType)) {
					builder.addPart(identifier, new StringBody(JSON.toJSONString(value), ContentType.APPLICATION_JSON));
				} else {
					// 默认作为字符串处理
					builder.addPart(identifier, new StringBody(value.toString(), ContentType.TEXT_PLAIN));
				}
			}
			try {
				return new StringEntity(EntityUtils.toString(builder.build()), UTF_8);
			} catch (UnsupportedCharsetException | ParseException | IOException e) {
				e.printStackTrace();
				log.error("createFormEntity异常:{}",e);
			}
		} else {
			// 使用 application/x-www-form-urlencoded
			StringBuilder formData = new StringBuilder();
			
			int i = 0;
			for (JSONObject field : fields) {
				String identifier = field.getString("identifier");
				Object value = field.get("value");
				
				if (value != null) {
				if (i > 0) {
					formData.append("&");
				}
					formData.append(identifier).append("=").append(value.toString());
					i++;
			}
		}
		StringEntity entity = new StringEntity(formData.toString(), UTF_8);
		entity.setContentType("application/x-www-form-urlencoded");
		return entity;
		}
		return null;
	}

	
	private String handleRequest(HttpApi httpApi,JSONObject params) {
		if(StrKit.equals("1", httpApi.getClientType())) {
			List<JSONObject> bodyParams = JSON.parseObject(httpApi.getRawContent(), new TypeReference<List<JSONObject>>() {});
			if(bodyParams!=null && bodyParams.size()>0) {
				JSONObject jsonObject=ParamFieldParseUtil.parseValuesByFields(bodyParams, JSONObject.toJSONString(params));
				if(jsonObject!=null && jsonObject.size()>0) {
					try {
						return IscUtil.getData(httpApi.getHost(),httpApi.getAppKey(),httpApi.getAppSecret(),httpApi.getCamsApi(),httpApi.getUrl(),JSONObject.toJSONString(params));
					} catch (Exception e) {
						log.error("PostStringArtemis 异常:{}",e.getMessage());
						return "";
					}
				}
			}
		}else {
			R<JSONObject> r= execute(httpApi,new JSONObject());
			if(200 != r.getCode()) {
				return "";
			}
			
			if(r.getData() == null) {
				return "";
			}
			
			if(!r.getData().containsKey("body")) {
				return "";
			}
			return r.getData().getString("body");
		}
		return "";
	}


	@Override
	public void executeTask(String dataLogId,String taskId,JSONObject flow) throws Exception {
		HttpApi httpApi = HttpApi.dao.findById(taskId);
		if(httpApi == null) {
			throw new Exception(String.format("%s任务数据已经不存在.", taskId));
		}
		
		String paginationType = httpApi.getPaginationType();
		if(StrKit.equals("0", paginationType)) {
			
			dataLogService.buildUpdateDataLog(dataLogId)
			.setInfo(String.format("%s开始执行任务", httpApi.getName()))
			.update(); 
			
			handlePage( dataLogId, httpApi,"",1,"",1, flow);
			
			
			List<DataLogInput> dataLogInputError=DataLogInput.dao.find("select id from "+DataLogInput.TABLE_NAME+" where log_id=? and status=?" ,dataLogId,DataLogStatusEnum.Error.getType());
			List<DataLogInput> dataLogInputWarning=DataLogInput.dao.find("select id from "+DataLogInput.TABLE_NAME+" where log_id=? and status=?" ,dataLogId,DataLogStatusEnum.Error.getType());

			dataLogService.buildUpdateDataLog(dataLogId)
			.setInfo(String.format("%s任务执行结束.", httpApi.getName()))
			.setError(dataLogInputError != null?dataLogInputError.size():0)
			.setWarning(dataLogInputWarning != null?dataLogInputWarning.size():0)
			.update(); 
			
			httpApi.setLastTime(new Date(System.currentTimeMillis()));
			httpApi.update();
		} else {
			String pageField = httpApi.getPageField();
			String pageSizeField = httpApi.getPageSizeField();
			String totalField = httpApi.getTotalField();
			int pageSize = httpApi.getPageSize();
			int pageNum = httpApi.getPageNum();
			
			// 先获取第一页数据和总数
			JSONObject params = new JSONObject();
			params.put(pageField, pageNum);
			params.put(pageSizeField, pageSize);
			
			
			dataLogService.buildUpdateDataLog(dataLogId)
			.setInfo(String.format("%s开始执任务", httpApi.getName()))
			.update(); 
			
			String firstPageResult = handlePage( dataLogId, httpApi, pageField, pageNum, pageSizeField, pageSize, flow);
			
			if(StrKit.isBlank(firstPageResult)) {
				dataLogService.buildUpdateDataLog(dataLogId)
				.setWarning(1)
				.setInfo(String.format("%s请求第%s页数据无响应，终止本次任务.", httpApi.getName(),pageNum))
				.update(); 
				throw new Exception(String.format("%s请求第%s页数据无响应，终止本次任务.", httpApi.getName(),pageNum));
			}
			
			dataLogService.buildUpdateDataLog(dataLogId)
			.setInfo(String.format("%s请求第%s页数据成功,开始解析数据量并计算分页数量.", httpApi.getName(),pageNum))
			.update(); 
			
			// 获取总数量
			JSONObject responseJson = JSON.parseObject(firstPageResult);
			int total = getValueByPath(responseJson, totalField.split("\\."));
			
			// 计算总页数
			int totalPages = (total + pageSize - 1) / pageSize;
			
			dataLogService.buildUpdateDataLog(dataLogId)
			.setInfo(String.format("%s计算分页结束，分页数量为:%s.", httpApi.getName(),totalPages))
			.update(); 
			
			if(totalPages > 1) {
				
				dataLogService.buildUpdateDataLog(dataLogId)
				.setInfo(String.format("%s创建线程池，线程数量:%s.", httpApi.getName(),httpApi.getThreads()))
				.update(); 
				
				// 创建线程池
				ExecutorService executorService = Executors.newFixedThreadPool(
					Math.min(totalPages - 1, httpApi.getThreads()) // 最大10个线程
				);
				
				List<Future<?>> futures = new ArrayList<>();
				
				// 从第2页开始异步处理
				for(int i = 2; i <= totalPages; i++) {
					final int currentPage = i;
					Future<?> future = executorService.submit(() -> {
						handlePage( dataLogId, httpApi, pageField, currentPage, pageSizeField, pageSize, flow);
					});
					futures.add(future);
				}
				// 等待所有任务完成
				try {
					dataLogService.buildUpdateDataLog(dataLogId)
					.setInfo(String.format("%s完成任务创建完成，等待futures:%s结束.", httpApi.getName(),futures.size()))
					.update(); 
					for(Future<?> future : futures) {
						future.get();
					}
					
					List<DataLogInput> dataLogInputError=DataLogInput.dao.find("select id from "+DataLogInput.TABLE_NAME+" where log_id=? and status=?" ,dataLogId,DataLogStatusEnum.Error.getType());
					List<DataLogInput> dataLogInputWarning=DataLogInput.dao.find("select id from "+DataLogInput.TABLE_NAME+" where log_id=? and status=?" ,dataLogId,DataLogStatusEnum.Error.getType());
					
					dataLogService.buildUpdateDataLog(dataLogId)
					.setError(dataLogInputError != null?dataLogInputError.size():0)
					.setWarning(dataLogInputWarning != null?dataLogInputWarning.size():0)
					.setInfo(String.format("%s完成任务创建等待，执行结束:%s.", httpApi.getName(),httpApi.getThreads()))
					.update(); 
				} catch (Exception e) {
					log.error("分页数据处理异常", e);
					throw new Exception(String.format("%s分页处理数据异常：%s.", httpApi.getName(),e.getMessage()));
				} finally {
					executorService.shutdown();
				}
			}
			httpApi.setLastTime(new Date(System.currentTimeMillis()));
			httpApi.update();
		}
	}
	
	
	private String handlePage(String dataLogId,HttpApi httpApi,String pageField,int currentPage,String pageSizeField,int pageSize,JSONObject flow) {
		JSONObject pageParams = new JSONObject();
		
		if(!StrKit.isBlank(pageField)) {
			pageParams.put(pageField, currentPage);
		}
		if(!StrKit.isBlank(pageSizeField)) {
			pageParams.put(pageSizeField, pageSize);
		}
		
		DataLogInput dataLogInput=dataLogService.createDataLogInput(dataLogId, currentPage, pageSize);
		
		dataLogService.
		buildUpdateDataLogInput(dataLogInput.getId()).
		setStatus(DataLogStatusEnum.Normal.getType()).
		setInfo(String.format("%s开始请求第%s页数据.", httpApi.getName(),currentPage)).
		update();
		
		String pageResult = handleRequest(httpApi, pageParams);
		if(!StrKit.isBlank(pageResult)) {
			try {
				dataLogService.
				buildUpdateDataLogInput(dataLogInput.getId()).
				setStatus(DataLogStatusEnum.Normal.getType()).
				setInfo(String.format("%s请求第%s页数据成功，开始执行数据处理流程.", httpApi.getName(),currentPage)).
				update();
				
				dataFlowService.execute(dataLogId,dataLogInput.getId(),flow, pageResult);
				
				dataLogService.
				buildUpdateDataLogInput(dataLogInput.getId()).
				setStatus(DataLogStatusEnum.Success.getType()).
				setInfo(String.format("%s第%s页数据处理结束.", httpApi.getName(),currentPage)).
				setEndTime(new Date(System.currentTimeMillis())).
				update();
			} catch (Exception e) {
				log.error("{}处理第{}页数据异常:{}", httpApi.getName() ,currentPage,e);
				
				dataErrorLogService.insertOrUpdateErrorLog(httpApi.getId(), httpApi.getName(), "HTTP", "error", e.getMessage());
				
				dataLogService.
				buildUpdateDataLogInput(dataLogInput.getId()).
				setStatus(DataLogStatusEnum.Error.getType()).
				setInfo(String.format("%s第%s页数据处理异常:%s.", httpApi.getName(),currentPage,e.getMessage())).
				setEndTime(new Date(System.currentTimeMillis())).
				update();
			}
		}else {
			dataLogService.
			buildUpdateDataLogInput(dataLogInput.getId()).
			setStatus(DataLogStatusEnum.Warning.getType()).
			setInfo(String.format("%s请求第%s页数据结束，无响应.", httpApi.getName(),currentPage)).
			update();
		}
		return pageResult;
		
	}
	
	
	/**
	 * 根据路径从JSON对象中获取值
	 * @param json JSON对象
	 * @param paths 路径数组
	 * @return 整数值
	 */
	private int getValueByPath(JSONObject json, String[] paths) {
		Object current = json;
		for (String path : paths) {
			if (current instanceof JSONObject) {
				current = ((JSONObject) current).get(path);
			} else {
				return 0;
			}
		}
		
		if (current instanceof Integer) {
			return (Integer) current;
		} else if (current instanceof String) {
			try {
				return Integer.parseInt((String) current);
			} catch (NumberFormatException e) {
				log.error("转换总数失败", e);
				return 0;
			}
		} else if (current instanceof Long) {
			return ((Long) current).intValue();
		}
		
		return 0;
	}
	
}
